package com.hjs.servlet.dailyinspection.dailyinspectiondamagenotice;

import com.hjs.bean.DailyInspectionDamageNotice;
import com.hjs.mapper.DailyInspectRecordingMapper;
import com.hjs.tools.Mapper;
import com.hjs.tools.RequestUtil;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author ShenXiaojian
 * @author violetfreesia
 * @date 2020/6/18
 * @date 2020/6/19 改
 **/
@Slf4j
@WebServlet("/dailyInspectionDamageNotice/update")
public class UpdateOneDailyInspectionDamageNotice extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        RequestUtil requestUtil = new RequestUtil(request, response);
        Mapper<DailyInspectRecordingMapper> dailyInspectRecordingMapperMapper = new Mapper<>(DailyInspectRecordingMapper.class);
        try {
            DailyInspectRecordingMapper dailyInspectRecordingMapper = dailyInspectRecordingMapperMapper.getMapper();
            boolean sqlResult;
            if (requestUtil.getString("auditStatus") != null) {
                sqlResult = dailyInspectRecordingMapper.updateDailyInspectionDamageNoticeAuditStatusById(requestUtil.getInt("damageNoticeID"),
                        requestUtil.getInt("auditStatus"), requestUtil.getString("auditOpinion"));
            } else {
                DailyInspectionDamageNotice dailyInspectionDamageNotice = new DailyInspectionDamageNotice(
                        requestUtil.getInt("damageNoticeID"), null,
                        requestUtil.getString("facilityName"), requestUtil.getString("damagePart"),
                        requestUtil.getString("damageReason"), null,
                        null, null,
                        null, null);
                sqlResult = dailyInspectRecordingMapper.updateOneDailyInspectionDamageNotice(dailyInspectionDamageNotice);
            }

            requestUtil.responseIsSuccess(sqlResult, "更新失败");
        } catch (Exception e) {
            log.error(e.toString());
            requestUtil.responseFail("未知错误");
        } finally {
            dailyInspectRecordingMapperMapper.closeSession();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) {
        doPost(request, response);
    }
}
